package org.bluweb.g4wicket.youtube;

import java.util.HashMap;
import java.util.Map;

import org.bluweb.g4wicket.G4WicketComponent;

@SuppressWarnings("serial")
public class YouTubePlayer extends G4WicketComponent {

	public enum AllowScriptAccess { always, never, sameDomain };
	public enum Quality { small, medium, large, hd720, hd1080, highres };
	
	private String movieId;
	private Quality quality = Quality.large;
	private boolean autoPlay;
	private Map<String,String> options = new HashMap<String, String>();
	private Map<String,String> attributes = new HashMap<String, String>();	
	
	public YouTubePlayer( String id, String movieId ) {
		super( id );
		this.addAttribute( "id", id );
		this.movieId = movieId;
		this.setAllowScriptAccess( YouTubePlayer.AllowScriptAccess.always );		
	}

	private YouTubePlayer addAttribute( String name, String value ) {
		this.attributes.put( name, '"' + value + '"' );
		return this;
	}

	private YouTubePlayer addAttribute( String name, boolean value ) {
		this.attributes.put( name, String.valueOf( value ) );
		return this;
	}
	
	private YouTubePlayer addOption( String name, String value ) {
		this.options.put( name, '"' + value + '"' );
		return this;
	}

	private YouTubePlayer addOption( String name, boolean value ) {
		this.options.put( name, String.valueOf( value ) );
		return this;
	}
	
	public YouTubePlayer setAllowScriptAccess( AllowScriptAccess value ) {
		return this.addOption( "allowScriptAccess", value.toString() );
	}

	public YouTubePlayer setQuality( Quality quality ) {
		this.quality = quality;
		return this;
	}
	
	public YouTubePlayer setAutoPlay( boolean autoPlay ) {
		this.autoPlay = autoPlay;
		return this;
	}
	
	@Override
	protected void onBeforeRender() {
		super.onBeforeRender();
		
		Map<String,Object> parameters = new HashMap<String,Object>();		
		parameters.put( "autoplay", this.autoPlay );
		parameters.put( "movieid", this.movieId );
		parameters.put( "quality", this.quality );
				
//		StringBuilder builder = new StringBuilder( "http://www.youtube.com/v/" );
//		builder.append( this.movieId );
		StringBuilder builder = new StringBuilder( "http://www.youtube.com/apiplayer" );
		builder.append( "?enablejsapi=1&version=3&playerapiid=" );
		builder.append( this.getId() );
		parameters.put( "playerurl", builder.toString() );
		
		builder = new StringBuilder( "{" );
		for( String name : attributes.keySet() ) {
			if( builder.length() > 1 ) {
				builder.append( "," );
			}
			builder.append( name.toLowerCase() ).append( ":" ).append( attributes.get( name ) ) ;
		}
		parameters.put( "attributes", builder.append( "}" ).toString() );

		builder = new StringBuilder( "{" );
		for( String name : options.keySet() ) {
			if( builder.length() > 1 ) {
				builder.append( "," );
			}
			builder.append( name ).append( ":" ).append( options.get( name ) ) ;
		}
		parameters.put( "options", builder.append( "}" ).toString() );		
		
		this.setJavaScriptTemplate( "YouTubePlayer.js", true, parameters );		
	}
}
